﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VPN.DataContext;
using System.Data.Linq;
using SWAS;

namespace VPN.Manage
{
    public class AccountManage
    {
        public static void Create(Account account)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                pdc.Accounts.InsertOnSubmit(account);
                pdc.SubmitChanges();
            }
        }

        public static void Update(Account account)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadAndWriteConnectionString))
            {
                pdc.Accounts.Attach(account, true);
                pdc.SubmitChanges();
            }
        }

        public static Account GetAccountByAccountName(string accountName)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadOnlyConnectionString))
            {
                pdc.ObjectTrackingEnabled = false;
                DataLoadOptions loads = new DataLoadOptions();
                loads.LoadWith<Account>(a => a.POs);
                pdc.LoadOptions = loads;
                return pdc.Accounts.FirstOrDefault(a => a.AccountName == accountName);
            }
        }

        public static Account GetAccountByAccountID(Guid accountID)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadOnlyConnectionString))
            {
                pdc.ObjectTrackingEnabled = false;
                DataLoadOptions loads = new DataLoadOptions();
                loads.LoadWith<Account>(a => a.POs);
                pdc.LoadOptions = loads;
                return pdc.Accounts.FirstOrDefault(a => a.AccountID == accountID);
            }
        }

        public static Account[] GetAccountList(string accountName, string userName, AccountStatus status, string typeCode, DateTime? expireStart, DateTime? expireEnd, IPagerInfo pi)
        {
            using (PODataContext pdc = new PODataContext(ConfigurationManager.ReadOnlyConnectionString))
            {
                pdc.ObjectTrackingEnabled = false;
                var accounts = pdc.Accounts.AsQueryable<Account>();

                if (!string.IsNullOrEmpty(accountName))
                    accounts = accounts.Where(a => a.AccountName.Contains(accountName));

                if (!string.IsNullOrEmpty(userName))
                    accounts = accounts.Where(a => a.UserName.Contains(userName));

                if ((int)status != 100)
                    accounts = accounts.Where(a => a.Status == (int)status);

                if (!string.IsNullOrEmpty(typeCode))
                    accounts = accounts.Where(a => a.Type == typeCode);

                if (expireStart != null)
                    accounts = accounts.Where(a => a.ExpireDate > expireStart);

                if (expireEnd != null)
                    accounts = accounts.Where(a => a.ExpireDate < expireEnd);

                if (pi != null)
                {
                    return accounts.Skip(pi.PageIndex * pi.PageSize).Take(pi.PageSize).ToArray();
                }
                else
                {
                    return accounts.ToArray();
                }
            }
        }



    }
}
